import * as echarts from 'echarts';
import 'echarts/extension/dataTool';

export const dataTypes = [
  { value: 'avg', label: '平均价差' },
  { value: 'min', label: '最低价差' },
  { value: 'q1', label: 'Q1价差' },
  { value: 'mid', label: '中位数价差' },
  { value: 'q3', label: 'Q3价差' },
  { value: 'max', label: '最高价差' },
];

export const getSeries1 = (_data) => {
  // 历史出清价差分布统计
  const list = Object.values(_data || {});
  if (!list.length) return null;
  let temp = [];
  let avg = [],
    min = [],
    q1 = [],
    mid = [],
    q3 = [],
    max = [];
  list.map((item) => {
    avg.push(item.avgPriceDifference); // 平均数
    temp.push(item?.priceDistribution || []);
  });
  let { boxData } = echarts?.dataTool.prepareBoxplotData(temp);
  boxData = boxData.map((x) => x.map((v) => v.toFixed(2)));
  boxData.map((x) => {
    min.push(x[0]);
    q1.push(x[1]);
    mid.push(x[2]);
    q3.push(x[3]);
    max.push(x[4]);
  });
  return {
    boxData,
    avg,
    min,
    q1,
    mid,
    q3,
    max,
  };
};

export const getSeries2 = (_data) => {
  // 市场供需对价差影响力统计
  const keys = Object.keys(_data || {});
  if (!keys.length) return null;
  const fmtVals = (k) => Object.values(_data[k]).map((v) => Math.round(v));
  return [
    {
      type: 'line',
      color: '#5E92F6',
      name: '价差与竞价空间差相关性',
      data: fmtVals('priceDifferenceBiddingSpace'),
    },
    {
      type: 'line',
      color: '#67DCAB',
      name: '价差与新能源差相关性',
      data: fmtVals('priceDifferenceNewEnergy'),
    },
    {
      type: 'line',
      color: '#F5BE15',
      name: '价差与联络线差相关性',
      data: fmtVals('priceDifferenceLink'),
    },
    {
      type: 'line',
      color: '#64779B',
      name: '价差与负荷差相关性',
      data: fmtVals('priceDifferenceLoad'),
    },
  ];
};
